Bitmap Pixel Collision

By: Rohan Varma

Introduction:

This tutorial will walk you through creating a method that will be able to detect when two images collide, not just their Rects. The tutorial assumes that you are able to load and draw two bitmaps onto the screen and that you are familiar with the Rect.intersect() method. If you are not, refer to the tutorials that teach you these things before continuing further.

 

Create the project

Create a new project and set it up so that there are two bitmaps moving around the screen

Detecting collision

                                   , Android will actually change r1 to the overlapping rectangle. Since we will need to know what r1 originally is, the first step is accomplished by creating a temporary rectangle and setting it equal to r1 and then using that to call the Rect.intersect() method. In order to do this your method should now look like this.https://lh6.googleusercontent.com/5Z39di1u6mEjegQraEHNXhKAqr4d1yxnxJkp4kVOhhaljRJvh7KGeXSail3F6V6lMogQ2jpxM6UPrp4KTmAMGJfox2P-1MNQp5E2dVVP6__LdM6sk0wjua_0L09GR_dlRhEXWRET

 

https://lh5.googleusercontent.com/5Xp5iZab0_8kkqgvAGOT3hbZgjIJp1XKnm0J4jl-kB-fbiCD_8vBVW8P677rLW0InQCaLn4Ed3_hruDEWhk8sbVQBMmpCkCMSTRtmQRnUuvhMnJc3SeEGtKSjMP1bH2QlQp7nDcs

https://lh5.googleusercontent.com/jKhkO4-DQqT1902tvkw8qsOPdo5ZcGg3rjpX5YDD8r5h8yPsD406iKmHQxI3o33Dx17UwI0yv18ZKfK6xFdsn1jVcXaweiz_0D2-QBwY3Z3PvW9Wnt9nt4Nqhc3i0LBXZBUuSerr

You can add this into any program, pass in the appropriate parameters, and see if two bitmaps collide, not just their Rects!